export const SUBJECT = new Set(['语文', '数学', '英语', '物理', '化学', '生物']);

export class Student {
    name = '';
    age = 18;
    sex = '';
    isBoarder = true;
    transcript = new Map();

    constructor(name, age, sex, isBoarder) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.isBoarder = isBoarder;
        this.transcript = new Map();
    }

    introduce() {
        let boarderStatus = this.isBoarder ? '住宿生' : '走读生';
        return `姓名${this.name},今年${this.age}岁,是一名${boarderStatus}`;
    }

    setSubjectScore(subject, score) {
        if (score < 0 && score > 100) {
            throw new Error();
        }
        this.transcript.set(subject, score);
    }
}

export function calTotalScore(student) {
    let total = 0;
    for (let score of student.transcript.values()) {
        total += score;
    }
    return total;
}

export function calAverageScore(totalScore, subjectCount) {
    if (subjectCount === 0) {
        throw new Error();
    }
    let averageScore = totalScore / subjectCount;
    let averageScoreStr = String(Math.round(averageScore * 100) / 100);
    return averageScoreStr;
}

// 7.异常处理
export let referenceErr = new ReferenceError("reference error");